<template>
  <div class="comment-page relative mx-auto max-w-7xl">
    <div class="mode">
      <div class="title flexbox flex-align-center">
        <h1 class="text capitalize">{{ $t('orderPage.ratingEvaluation') }}</h1>
      </div>
    </div>
    <!-- <div
      class="top-[5.625rem] sticky p-1 overflow-hidden bg-red-200 rounded-md"
    >
      <div class="p-5 text-xl font-bold bg-red-300 rounded-md">lllll</div>
    </div> -->
    <div class="cont flexbox">
      <div class="left h-fit lg:sticky top-[7rem]">
        <nuxt-link class="goods flexbox flex-align-center cursor-pointer" :to="localePath(`/product/${detailInfo.code}`)" v-if="detailInfo">
          <div class="img overflow-hidden flex-none border">
            <img
              loading="lazy"
              class="size-full object-cover"
              v-lazy="$globalFn.imgPrefixUrl(detailInfo.image, 'goods_thumb_1_1')"
              :alt="detailInfo.goods_name"
              v-if="detailInfo.image"
            />
          </div>
          <div class="base">
            <h1 class="name">{{ detailInfo.goods_name }}</h1>
            <div class="desc" v-if="detailInfo.region && detailInfo.region.name">
              {{ detailInfo.region.name }}
            </div>
          </div>
        </nuxt-link>
        <div class="score-num">
          <div class="num flexbox flex-align-center flex-justify-between">
            <span class="score">{{ scoreInfo.avg }}</span>
            <span class="comment"> {{ totalScore }} {{ $t('orderPage.score') }}</span>
          </div>
          <div class="get-star flexbox flex-align-center">
            <div class="star-item" v-for="item in 5" :key="item">
              <img
                loading="lazy"
                class="star-icon"
                v-if="item > scoreInfo.avg"
                src="https://cd.sealmg.com/assets/img/star-gray.png"
                alt="Empty star rating"
                title="Unselected rating star"
                srcset=""
              />
              <img
                loading="lazy"
                class="star-icon"
                v-else
                src="https://cd.sealmg.com/assets/img/star.png"
                alt="Filled star rating"
                title="Selected rating star"
                srcset=""
              />
              <!-- <img class="star-icon"  src="https://cd.sealmg.com/assets/img/star-half.png" alt="" srcset=""> -->
            </div>
          </div>
        </div>
        <div class="score-rate">
          <div class="star-rate flexbox flex-align-center" v-for="(item, index) in scoreInfo.scores" :key="index">
            <div class="star-box flexbox flex-align-center flex-justify-end">
              <img
                loading="lazy"
                class="star-icon"
                v-for="num in item.score"
                :key="num"
                src="https://cd.sealmg.com/assets/img/star-mini.png"
                alt="Rating star"
                title="Product rating star"
                srcset
              />
            </div>
            <div class="progress flex-1">
              <el-progress :percentage="item.percent" :stroke-width="4" :show-text="false" color="#FF4E33" define-back-color="#FFD4C4"></el-progress>
            </div>
            <div class="rate">{{ item.percent }}%</div>
          </div>
        </div>
      </div>
      <div class="right w-full">
        <div class="evaluate">
          <div class="tabs flexbox flex-align-center">
            <div :class="['tab capitalize', tabId == '' ? 'active' : '']" @click="changeTab('')">
              {{ $t('filter.all') }}
            </div>
            <div :class="['tab capitalize', tabId == 5 ? 'active' : '']" @click="changeTab(5)">
              {{ $t('filter.excellent') }}
            </div>
            <div :class="['tab capitalize', tabId == 3 ? 'active' : '']" @click="changeTab(3)">
              {{ $t('filter.commonly') }}
            </div>
            <div :class="['tab capitalize', tabId == 1 ? 'active' : '']" @click="changeTab(1)">
              {{ $t('filter.poorRating') }}
            </div>
          </div>
          <div class="list flexbox">
            <InfiniteScroll :loading="loading" :no-more="noMore" @load-more="loadMoreData" v-model="evaluateList">
              <div class="md:grid-cols-2 grid grid-cols-1 gap-4 py-4 w-full">
                <div class="item" v-for="(item, index) in evaluateList" :key="index">
                  <div class="info flexbox flex-justify-between">
                    <div class="avator flexbox">
                      <img
                        loading="lazy"
                        class="img object-cover"
                        v-lazy="$globalFn.imgPrefixUrl(item.avatar, 'goods_thumb_1_1')"
                        :alt="item.nickname"
                        :title="item.nickname"
                        srcset=""
                      />
                      <div class="name">
                        <div class="text">{{ item.nickname }}</div>
                        <div class="stars flexbox flex-align-center">
                          <div class="star" v-for="num in 5" :key="num">
                            <img
                              loading="lazy"
                              class="icon"
                              v-if="item.score < num"
                              src="https://cd.sealmg.com/assets/img/star-gray.png"
                              alt="Empty rating star"
                              title="Unrated star"
                            />
                            <img
                              loading="lazy"
                              class="icon"
                              v-else
                              src="https://cd.sealmg.com/assets/img/star-mini.png"
                              alt="Filled rating star"
                              title="Rated star"
                            />
                            <!-- <img class="icon" src="https://cd.sealmg.com/assets/img/star-mini-half.png" alt="" srcset=""> -->
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="date">
                      {{ item.create_time_text.split(' ')[0] }}
                    </div>
                  </div>
                  <div class="desc three-line">{{ item.content }}</div>
                </div>
              </div>
            </InfiniteScroll>
          </div>
          <!-- <div class="mt-8"></div> -->
        </div>
      </div>
    </div>
    <div class="h-[80px]"></div>
  </div>
</template>
<script>
  export default {
    layout: 'newsHome',
    name: 'orderDetail',
    key(route) {
      return route.fullPath
    },
    head() {
      return {
        title: `Reviews - ${this.detailInfo.goods_name} - SEALMG`,
        meta: [
          {
            hid: 'description',
            name: 'description',
            content: `More About ${this.detailInfo.goods_name} Reviews`
          }
        ]
      }
    },
    data: function () {
      return {
        detailId: '', // 商品详情ID
        detailInfo: {}, // 商品详情信息对象
        tabId: '', // 当前选中的评价标签ID（空表示全部评价）
        scoreInfo: {}, // 评分统计信息对象
        totalScore: 0, // 评价总数
        loading: false, // 加载状态
        evaluateList: [], // 评价列表数据
        loading: false, // 加载状态
        noMore: false, // 是否没有更多数据
        page: 1, // 当前页码
        limit: 10 // 每页数量
      }
    },
    created() {},
    async asyncData({ app, params }) {
      try {
        // 并行请求多个API以提高性能
        const [detailRes, scoreRes, evaluateRes] = await Promise.all([
          app.$axios.get(`/api/goods/read?id=${params.id}`),
          app.$axios.get(`/api/comment/total?id=${params.id}`),
          app.$axios.get(`/api/comment/index?id=${params.id}&limit=20`)
        ])
        // 计算总评价数
        const totalScore = scoreRes?.scores?.reduce((sum, item) => sum + item.count, 0) || 0
        return {
          detailInfo: detailRes,
          detailId: params,
          loading: false,
          scoreInfo: scoreRes || {},
          totalScore,
          evaluateList: evaluateRes || []
        }
      } catch (error) {
        console.error('获取评论数据失败:', error)
        return {
          detailInfo: {},
          detailId: params,
          loading: false,
          scoreInfo: {},
          totalScore: 0,
          evaluateList: []
        }
      }
    },
    methods: {
      loadMoreData() {
        if (!this.loading && !this.noMore) {
          this.page++
          this.getEvaluateList()
        }
      },
      async getEvaluateList() {
        this.loading = true
        try {
          const res = await this.$axios.get(`/api/comment/index`, {
            params: {
              page: this.page,
              id: this.detailInfo.id,
              limit: this.limit,
              score: this.tabId
            }
          })
          // 如果是第一页，直接替换数据
          if (this.page === 1) {
            this.evaluateList = res || []
          } else {
            // 否则追加数据
            this.evaluateList = [...this.evaluateList, ...(res || [])]
          }
          // 判断是否还有更多数据
          this.noMore = res.length === 0 || res.length < this.limit
        } catch (err) {
          console.error('获取评价列表失败:', err)
          this.evaluateList = []
          this.noMore = true
        } finally {
          this.loading = false
        }
      },
      changeTab(type) {
        this.page = 1
        this.tabId = type
        this.loading = true
        this.evaluateList = []
        this.getEvaluateList()
      }
    }
  }
</script>
<style lang="less" scoped>
  .comment-page {
    .mode {
      box-sizing: border-box;
      margin-top: 20px;
      padding: 20px 24px 11px 24px;
      .title {
        padding-bottom: 8px;
        border-bottom: @border-1-e5;
        .text {
          font-weight: 500;
          font-size: 22px;
          color: @font-color-14;
        }
      }
    }
    .cont {
      width: 100%;
      margin-top: 8px;
      .left {
        box-sizing: border-box;
        padding: 20px 24px;
        width: 387px;
        .goods {
          .img {
            width: 88px;
            height: 88px;
            border-radius: 16px;
          }
          .base {
            margin-left: 16px;
            .name {
              font-weight: 500;
              font-size: 16px;
              color: @font-color-26;
            }
            .desc {
              margin-bottom: 6px;
              font-size: 12px;
              color: @font-color-66;
            }
          }
        }
        .score-num {
          margin-top: 28px;
          .num {
            .score {
              font-family: Manrope, Manrope;
              font-weight: bold;
              font-size: 32px;
              color: @font-color-26;
            }
            .comment {
              font-weight: 500;
              font-size: 14px;
              color: @font-color-66;
            }
          }
          .get-star {
            margin-top: 26px;
            .star-item {
              width: 20px;
              height: 20px;
              margin-left: 8px;
              .star-icon {
                width: 20px;
                height: 20px;
              }
            }
            .star-item:first-child {
              margin-left: 0;
            }
          }
        }
        .score-rate {
          margin-top: 26px;
          .star-rate {
            width: 339px;
            .star-box {
              width: 76px;
              .star-icon {
                width: 12px;
                height: 12px;
                margin-left: 4px;
              }
              .star-icon:first-child {
                margin-left: 0px;
              }
            }
          }
          .progress {
            margin-left: 8px;
          }
          .rate {
            width: 30px;
            margin-left: 16px;
            font-family: Manrope, Manrope;
            font-weight: 500;
            font-size: 12px;
            color: #b3b3b3;
          }
        }
      }
      .right {
        box-sizing: border-box;
        margin-left: 20px;
        // width: 793px;
        .evaluate {
          .tabs {
            height: 60px;
            border-bottom: @border-1-fo;
            .tab {
              padding: 0 24px;
              line-height: 60px;
              font-size: 16px;
              color: @font-color-99;
              cursor: pointer;
            }
            .active {
              position: relative;
              font-weight: 500;
              color: @theme-color;
            }
            .active::after {
              content: '';
              display: block;
              position: absolute;
              left: 50%;
              bottom: 0;
              width: 64px;
              height: 2px;
              background: @theme-color;
              transform: translateX(-50%);
            }
          }
          .list {
            .item {
              box-sizing: border-box;
              padding: 20px;
              // width: 365px;
              // margin-left: 40px;
              // margin-top: 20px;
              border-radius: 16px;
              background: @color-f7;
              .info {
                .avator {
                  .img {
                    width: 36px;
                    height: 36px;
                    border-radius: 50%;
                  }
                  .name {
                    margin-left: 8px;
                    .text {
                      font-weight: 500;
                      font-size: 14px;
                      color: @font-color-26;
                    }
                    .stars {
                      .star {
                        width: 12px;
                        height: 12px;
                        margin-left: 4px;
                        .icon {
                          width: 12px;
                          height: 12px;
                        }
                      }
                      .star:first-child {
                        margin-left: 0;
                      }
                    }
                  }
                }
                .date {
                  margin-top: 8px;
                  font-family: HelveticaNeue, HelveticaNeue;
                  font-weight: 500;
                  font-size: 12px;
                  color: @font-color-99;
                }
              }
              .desc {
                margin-top: 12px;
                font-weight: 400;
                font-size: 12px;
                color: @font-color-26;
                line-height: 20px;
                min-height: 66px;
              }
            }
            .item:nth-child(2n + 1) {
              margin-left: 0;
            }
          }
        }
      }
    }
  }
  @media only screen and (min-width: 1200px) {
  }
  @media screen and (max-width: 992px) and (min-width: 768px) {
    .comment-page {
      .mode {
        box-sizing: border-box;
        margin-top: 0px;
        padding: 16px 16px 8px 16px;
        .title {
          padding-bottom: 8px;
          border-bottom: @border-1-e5;
          .text {
            font-weight: 500;
            font-size: 20px;
            color: @font-color-14;
          }
        }
      }
      .cont {
        margin-top: 8px;
        display: block;
        .left {
          box-sizing: border-box;
          padding: 0px 16px;
          width: 100%;
          .goods {
            .img {
              width: 56px;
              height: 56px;
              border-radius: 8px;
            }
            .base {
              margin-left: 12px;
              .name {
                font-weight: 500;
                font-size: 14px;
                color: @font-color-14;
              }
              .desc {
                margin-top: 3px;
                font-size: 12px;
                color: @font-color-99;
                margin-bottom: 0;
              }
            }
          }
          .score-num {
            margin-top: 16px;
            .num {
              .score {
                font-family: Manrope, Manrope;
                font-weight: bold;
                font-size: 24px;
                color: @font-color-26;
              }
              .comment {
                font-weight: 500;
                font-size: 12px;
                color: @font-color-66;
              }
            }
            .get-star {
              margin-top: 0px;
              .star-item {
                width: 12px;
                height: 12px;
                margin-left: 4px;
                .star-icon {
                  width: 12px;
                  height: 12px;
                }
              }
              .star-item:first-child {
                margin-left: 0;
              }
            }
          }
          .score-rate {
            margin-top: 16px;
            .star-rate {
              width: 100%;
              .star-box {
                width: 48px;
                .star-icon {
                  width: 8px;
                  height: 8px;
                  margin-left: 2px;
                }
                .star-icon:first-child {
                  margin-left: 0px;
                }
              }
            }
            .progress {
              margin-left: 16px;
            }
            .rate {
              width: 28px;
              margin-left: 10px;
              font-family: Manrope, Manrope;
              font-weight: 500;
              font-size: 10px;
              text-align: left;
              color: #b3b3b3;
            }
          }
        }
        .right {
          box-sizing: border-box;
          margin-left: 0px;
          width: 100%;
          padding-bottom: 32px;
          .evaluate {
            .tabs {
              display: none;
            }
            .list {
              display: block;
              padding: 0 16px;
              .item {
                box-sizing: border-box;
                padding: 12px 10px;
                width: 100%;
                margin-left: 0px;
                margin-top: 16px;
                border-radius: 12px;
                background: @color-f7;
                .info {
                  .avator {
                    .img {
                      width: 36px;
                      height: 36px;
                      border-radius: 50%;
                    }
                    .name {
                      margin-left: 8px;
                      .text {
                        font-weight: 500;
                        font-size: 14px;
                        color: @font-color-26;
                      }
                      .stars {
                        .star {
                          width: 12px;
                          height: 12px;
                          margin-left: 4px;
                          .icon {
                            width: 12px;
                            height: 12px;
                          }
                        }
                        .star:first-child {
                          margin-left: 0;
                        }
                      }
                    }
                  }
                  .date {
                    margin-top: 0px;
                    font-family: HelveticaNeue, HelveticaNeue;
                    font-weight: 500;
                    font-size: 12px;
                    color: @font-color-99;
                  }
                }
                .desc {
                  margin-top: 10px;
                  font-weight: 400;
                  font-size: 12px;
                  color: @font-color-26;
                  line-height: 20px;
                  min-height: auto;
                }
              }
              .item:nth-child(2n + 1) {
                margin-left: 0;
              }
            }
          }
        }
      }
    }
  }
  @media screen and (max-width: 767px) {
    .comment-page {
      .mode {
        box-sizing: border-box;
        margin-top: 0px;
        padding: 16px 16px 8px 16px;
        .title {
          padding-bottom: 8px;
          border-bottom: @border-1-e5;
          .text {
            font-weight: 500;
            font-size: 20px;
            color: @font-color-14;
          }
        }
      }
      .cont {
        margin-top: 8px;
        display: block;
        .left {
          box-sizing: border-box;
          padding: 0px 16px;
          width: 100%;
          .goods {
            .img {
              width: 56px;
              height: 56px;
              border-radius: 8px;
            }
            .base {
              margin-left: 12px;
              .name {
                font-weight: 500;
                font-size: 14px;
                color: @font-color-14;
              }
              .desc {
                margin-top: 3px;
                font-size: 12px;
                color: @font-color-99;
                margin-bottom: 0;
              }
            }
          }
          .score-num {
            margin-top: 16px;
            .num {
              .score {
                font-family: Manrope, Manrope;
                font-weight: bold;
                font-size: 24px;
                color: @font-color-26;
              }
              .comment {
                font-weight: 500;
                font-size: 12px;
                color: @font-color-66;
              }
            }
            .get-star {
              margin-top: 0px;
              .star-item {
                width: 12px;
                height: 12px;
                margin-left: 4px;
                .star-icon {
                  width: 12px;
                  height: 12px;
                }
              }
              .star-item:first-child {
                margin-left: 0;
              }
            }
          }
          .score-rate {
            margin-top: 16px;
            .star-rate {
              width: 100%;
              .star-box {
                width: 48px;
                .star-icon {
                  width: 8px;
                  height: 8px;
                  margin-left: 2px;
                }
                .star-icon:first-child {
                  margin-left: 0px;
                }
              }
            }
            .progress {
              margin-left: 16px;
            }
            .rate {
              width: 28px;
              margin-left: 10px;
              font-family: Manrope, Manrope;
              font-weight: 500;
              font-size: 10px;
              text-align: left;
              color: #b3b3b3;
            }
          }
        }
        .right {
          box-sizing: border-box;
          margin-left: 0px;
          width: 100%;
          padding-bottom: 32px;
          .evaluate {
            .tabs {
              display: none;
            }
            .list {
              display: block;
              padding: 0 16px;
              .item {
                box-sizing: border-box;
                padding: 12px 10px;
                width: 100%;
                margin-left: 0px;
                margin-top: 16px;
                border-radius: 12px;
                background: @color-f7;
                .info {
                  .avator {
                    .img {
                      width: 36px;
                      height: 36px;
                      border-radius: 50%;
                    }
                    .name {
                      margin-left: 8px;
                      .text {
                        font-weight: 500;
                        font-size: 14px;
                        color: @font-color-26;
                      }
                      .stars {
                        .star {
                          width: 12px;
                          height: 12px;
                          margin-left: 4px;
                          .icon {
                            width: 12px;
                            height: 12px;
                          }
                        }
                        .star:first-child {
                          margin-left: 0;
                        }
                      }
                    }
                  }
                  .date {
                    margin-top: 0px;
                    font-family: HelveticaNeue, HelveticaNeue;
                    font-weight: 500;
                    font-size: 12px;
                    color: @font-color-99;
                  }
                }
                .desc {
                  margin-top: 10px;
                  font-weight: 400;
                  font-size: 12px;
                  color: @font-color-26;
                  line-height: 20px;
                  min-height: auto;
                }
              }
              .item:nth-child(2n + 1) {
                margin-left: 0;
              }
            }
          }
        }
      }
    }
  }
</style>
